package com.company.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.base.common.persistence.Page;
import com.base.common.utils.StringUtils;
import com.company.model.CompanyDevice;
import com.company.model.CompanyEmployee;
import com.company.model.CompanyEmployeeFee;
import com.company.service.DeviceService;

@Controller
public class DeviceController {
	public static final String toDeviceView = "/company/device";
	public static final String deviceList = "/company/device/list";
	public static final String openDeviceEditView = "/company/device/editView";
	public static final String deviceDelete = "/company/device/delete";
	public static final String openDeviceAddView = "/company/device/addView";
	public static final String deviceSave = "/company/device/save";
	// 页面路径
	public static final String devicePage = "/company/device";
	public static final String deviceListPage = "/company/deviceList";
	public static final String deviceEditPage = "/company/deviceEdit";
	public static final String deviceAddPage = "/company/deviceAdd";

	@Autowired
	DeviceService deviceService;

	@RequestMapping(value = toDeviceView, method = RequestMethod.POST)
	public ModelAndView toDeviceView(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mv = new ModelAndView(devicePage);
		String companyId = req.getSession().getAttribute("companyId").toString();
		List<CompanyDevice> list = null;
		try {
			list = deviceService.findDeviceListByCompanyId(companyId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("deviceList", list);
		return mv;
	}

	@RequestMapping(value = deviceList, method = RequestMethod.POST)
	public ModelAndView deviceList(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mv = new ModelAndView(deviceListPage);
		String companyId = req.getSession().getAttribute("companyId").toString();
		List<CompanyDevice> list = null;
		try {
			list = deviceService.findDeviceListByCompanyId(companyId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("deviceList", list);
		return mv;
	}

	@RequestMapping(value = openDeviceEditView, method = RequestMethod.POST)
	public ModelAndView openDeviceEditView(HttpServletRequest req, HttpServletResponse res, String id) {
		ModelAndView mv = new ModelAndView(deviceEditPage);
		CompanyDevice companyDevice = null;
		try {
			companyDevice = deviceService.findDeviceById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("device", companyDevice);
		return mv;
	}

	@RequestMapping(value = openDeviceAddView, method = RequestMethod.POST)
	public ModelAndView openDeviceAddView(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mv = new ModelAndView(deviceAddPage);
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = deviceDelete, method = RequestMethod.POST)
	public boolean deviceDelete(String ids) {
		boolean result = false;
		try {
			result = deviceService.deleteDeviceByIds(ids);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = deviceSave, method = RequestMethod.POST)
	public boolean deviceSave(HttpServletRequest req, HttpServletResponse res, CompanyDevice companyDevice) {
		String companyId = req.getSession().getAttribute("companyId").toString();
		boolean result = false;
		try {
			companyDevice.setCompanyId(companyId);
			companyDevice.setCreateTime(new Date());
			result = deviceService.saveDevice(companyDevice);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
